package com.digital.community.common.utils;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.ByteArrayBuffer;
import org.apache.http.util.EncodingUtils;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;



public class ConnectWeb
{
	
	public static String test(String URL, List<BasicNameValuePair> params) { 
        HttpPost httpPost = new HttpPost(URL); 
        String returnString = ""; 
        HttpParams httpParameters = new BasicHttpParams(); 
 
        try {
            UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity( 
                    params, "UTF-8");
            httpPost.setEntity(urlEncodedFormEntity); 
            HttpConnectionParams.setConnectionTimeout(httpParameters, 3000); 
            HttpClient httpClient = new DefaultHttpClient(httpParameters); 
            HttpResponse httpResponse = httpClient.execute(httpPost); 
            HttpEntity httpEntity = httpResponse.getEntity(); 
            InputStream inputStream = httpEntity.getContent(); 
            BufferedInputStream bufferedInputStream = new BufferedInputStream( 
                    inputStream); 
            ByteArrayBuffer byteArrayBuffer = new ByteArrayBuffer(50); 
            int current = 0; 
            while ((current = bufferedInputStream.read()) != -1) { 
                byteArrayBuffer.append(current); 
            } 
            returnString = EncodingUtils.getString(byteArrayBuffer 
                    .toByteArray(), "GBK"); 
        } catch (UnsupportedEncodingException e) { 
            e.printStackTrace(); 
        } catch (ClientProtocolException e) { 
            e.printStackTrace(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
        return returnString; 
    } 
	
	public static String httpPost(String URL, String title, String param) { 
        HttpPost httpPost = new HttpPost(URL); 
        String returnString = ""; 
        HttpParams httpParameters = new BasicHttpParams(); 
      //使用NameValuePair来保存要传递的Post参数            
        List<NameValuePair> params = new ArrayList<NameValuePair>();            
        //添加要传递的参数            
        if(title!=""){
        	params.add(new BasicNameValuePair(title, param));
        }
        try { 
            UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity( 
                    params, "UTF-8"); 
            httpPost.setEntity(urlEncodedFormEntity); 
            HttpConnectionParams.setConnectionTimeout(httpParameters, 3000); 
            HttpClient httpClient = new DefaultHttpClient(httpParameters);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity(); 
            InputStream inputStream = httpEntity.getContent(); 
            BufferedInputStream bufferedInputStream = new BufferedInputStream( 
                    inputStream); 
            ByteArrayBuffer byteArrayBuffer = new ByteArrayBuffer(50); 
            int current = 0; 
            while ((current = bufferedInputStream.read()) != -1) { 
                byteArrayBuffer.append(current); 
            } 
            returnString = EncodingUtils.getString(byteArrayBuffer 
                    .toByteArray(), "GBK"); 
        } catch (UnsupportedEncodingException e) { 
            e.printStackTrace(); 
            return "";
        } catch (ClientProtocolException e) { 
            e.printStackTrace(); 
            return "";
        } catch (IOException e) { 
            e.printStackTrace(); 
            return "";
        } 
        return returnString; 
    } 
	
	


}



